//
//  XHVoteViewController.m
//  xinhua_sx
//
//  Created by 党阳阳 on 14-7-16.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//





#import "XHVoteViewController.h"
#import "AFNetworking.h"
#import "MBProgressHUD.h"
@interface XHVoteViewController()
{
    MBProgressHUD* HUD;
}
@property (nonatomic, strong) UIPageControl* pageControl;
@end
@implementation XHVoteViewController
@synthesize scrollView = _scrollView;


-(void)viewDidLoad
{
    [super viewDidLoad];
     CGFloat oY = kIOS_VERSION<7.0?44:64;
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, oY-20, 320, self.view.frame.size.height-64)];
    _scrollView.scrollEnabled = YES;
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.backgroundColor = [UIColor grayColor];
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    UIPageControl* pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-10, 320, 10)];
    pageControl.backgroundColor = [UIColor grayColor];
    pageControl.alpha = 0.5;

    //[self.view addSubview:pageControl];
    self.pageControl = pageControl;
    [pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
    //[self loadI];
    [self performSelector:@selector(loadI) withObject:nil afterDelay:1.0];
    
}

-(void)pageChanged:(UIPageControl*)pc{
    NSArray *subViews = pc.subviews;
    for (int i = 0; i < [subViews count]; i++) {
        UIView* subView = [subViews objectAtIndex:i];
        if ([NSStringFromClass([subView class]) isEqualToString:NSStringFromClass([UIImageView class])]) {
            ((UIImageView*)subView).image = (pc.currentPage == i ? [UIImage imageNamed:@"homepage_listview_item_point.png"] : [UIImage imageNamed:@"BluePoint.png"]);
           // subView.backgroundColor =(pc.currentPage == i ? [UIColor blueColor] : [UIColor redColor]);
           //file:///Users/love/Desktop/xinhua2.0/xinhua_sx/Home/homepage_listview_item_point.png
        }
        
    }
}
-(void)loadI
{
    NSString* UID = [[NSUserDefaults standardUserDefaults] objectForKey:@"UID"];
    if (!UID)
    {
        UIAlertView* viw = [[UIAlertView alloc] initWithTitle:@"提示" message:@"需要登录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [viw show];
        return;
    }
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    HUD.labelText = @"正在加载";
    HUD.dimBackground = YES;
    [self.view addSubview:HUD];
    [HUD show:YES];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    NSString* sting = [NSString stringWithFormat:@"http://183.203.18.45:7001/xinhua/m/getPostTitle.html"];
    [manager GET:sting parameters:nil success:^(AFHTTPRequestOperation*operation, id responseObject) {
        id jsonObject = [NSJSONSerialization JSONObjectWithData:operation.responseData
                                                        options:NSJSONReadingAllowFragments
                                                          error:nil];
        NSLog(@"json = %@",jsonObject);
        NSLog(@"jsonc class = %@  count = %d",[jsonObject class], [jsonObject count]);
        [HUD hide:YES];
        for (int i = 0; i < [jsonObject count]; i ++)
        {
            toupiao* view = [[toupiao alloc] initWithDic:jsonObject[i]];
            view.frame = CGRectMake(320*i, 0, 320, _scrollView.frame.size.height);
            view.block = ^(NSString* str,NSString* st)
            {
                NSLog(@"id = %@",str);
                
               // [self toupiao:str tit:st];
                NSString* UID = [[NSUserDefaults standardUserDefaults] objectForKey:@"UID"];
                if (!UID)
                {
                    UIAlertView* viw = [[UIAlertView alloc] initWithTitle:@"提示" message:@"需要登录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [viw show];
                    return ;
                }
                [HUD show:YES];
                AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
                manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
                
                NSDictionary* dic =@{@"postChooseId":str,@"postTitleId":st,@"userId":UID};
                NSString* sting = [NSString stringWithFormat:@"http://183.203.18.45:7001/xinhua/m/setPostChooseHits.html"];
                [manager POST:sting parameters:dic success:^(AFHTTPRequestOperation*operation, id responseObject) {
                    id jsonObject = [NSJSONSerialization JSONObjectWithData:operation.responseData
                                                                    options:NSJSONReadingAllowFragments
                                                                      error:nil];
                    [HUD hide:YES];
                    
                    NSLog(@"json = %@",jsonObject);
                    NSString* string = [NSString stringWithFormat:@"%@",[jsonObject objectForKey:@"result"]];
                    if([string isEqualToString:@"success"])
                    {
                        UIAlertView* view1 = [[UIAlertView alloc] initWithTitle:@"提示" message:jsonObject[@"message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
                       // [view1 show];
                        //[view setFF];
                        
                        
                    }
                    else
                    {
                        UIAlertView* view1 = [[UIAlertView alloc] initWithTitle:@"提示" message:jsonObject[@"message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
                       // [view1 show];
                        
                        
                    }
                    
                }
                failure:^(AFHTTPRequestOperation *operation, NSError*error) {
                          [HUD hide: YES];
                          NSLog(@"error = %@",error);
                          
                      }];

         
            };
            [_scrollView addSubview:view];
            _scrollView.contentSize = CGSizeMake(320*(i+1), 0);
            [self.view addSubview:self.pageControl];
            self.pageControl.numberOfPages = [jsonObject count];
            self.pageControl.currentPage  = 0;
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError*error) {
        [HUD hide:YES];
        NSLog(@"error = %@",error);
    }];

    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([scrollView isEqual:self.scrollView]) {
        
        NSInteger page = fabs(scrollView.contentOffset.x/self.scrollView.frame.size.width);
        _pageControl.currentPage = page;
        
    }
}

-(void)toupiao:(NSString*)chousID tit:(NSString*)titltId
{
    NSString* UID = [[NSUserDefaults standardUserDefaults] objectForKey:@"UID"];
    if (!UID)
    {
        UIAlertView* viw = [[UIAlertView alloc] initWithTitle:@"提示" message:@"需要登录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [viw show];
        return ;
    }
    [HUD show:YES];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    
    NSDictionary* dic =@{@"postChooseId":chousID,@"postTitleId":titltId,@"userId":UID};
    NSString* sting = [NSString stringWithFormat:@"http://183.203.18.45:7001/xinhua/m/setPostChooseHits.html"];
    [manager POST:sting parameters:dic success:^(AFHTTPRequestOperation*operation, id responseObject) {
        id jsonObject = [NSJSONSerialization JSONObjectWithData:operation.responseData
                                                        options:NSJSONReadingAllowFragments
                                                          error:nil];
        [HUD hide:YES];
        
        NSLog(@"json = %@",jsonObject);
        NSString* string = [NSString stringWithFormat:@"%@",[jsonObject objectForKey:@"result"]];
        if([string isEqualToString:@"success"])
        {
            UIAlertView* view = [[UIAlertView alloc] initWithTitle:@"提示" message:jsonObject[@"message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [view show];
            
        }
        else
        {
            UIAlertView* view = [[UIAlertView alloc] initWithTitle:@"提示" message:jsonObject[@"message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [view show];
          

        }
       
        }
        failure:^(AFHTTPRequestOperation *operation, NSError*error) {
            [HUD hide: YES];
        NSLog(@"error = %@",error);
            
    }];

}
@end
